package org.jetnuke.modules.statistics;

//import java.awt.Color;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.BrowserCounterDAO;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.OsCounterDAO;
import org.jetnuke.entities.Browser_counter;
import org.jetnuke.entities.OsCounter;


public class BrowserStatistics implements IAction {

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		DecimalFormat df=new DecimalFormat("###.##");
		BrowserCounterDAO bcm=DataAccessDriver.getInst().newBrowserCounterDAO();
		int total=bcm.getTotalCount();
		HashMap perCent=new HashMap();
		HashMap numbers=new HashMap();
		if(total!=0){
			Iterator it=bcm.selectAll().iterator();
			while(it.hasNext()){
				Browser_counter bc=(Browser_counter)it.next();
				String browser=bc.getBID();
				int count=bc.getCOUNTER();
				if(!"Other".equals(browser)){
					vc.put(browser,new Integer(count*400/total));
				}else{
					vc.put("Other1",new Integer(count*400/total));
				}
				String str1=df.format(count*100./total);
				perCent.put(browser,str1);
				numbers.put(browser,String.valueOf(count));
			}
			vc.put("perCent",perCent);
			vc.put("numbers",numbers);
		}
		OsCounterDAO ocm=DataAccessDriver.getInst().newOsCounterDAO();
		total=ocm.getTotalCount();
		HashMap perCentOS=new HashMap();
		HashMap numbersOS=new HashMap();
		if(total>0){
			Iterator it=ocm.selectAll().iterator();
			while(it.hasNext()){
				OsCounter oc=(OsCounter)it.next();
				String os=oc.getOID();
				if(os.equals("OS/2")){
					os="OS_2";
				}
				int count=oc.getCOUNTER();
				vc.put(os,new Integer(count*400/total));
				String str1=df.format(count*100./total);
				perCentOS.put(os,str1);
				numbersOS.put(os,String.valueOf(count));
			}
			vc.put("perCentOS",perCentOS);
			vc.put("numbersOS",numbersOS);
		}
		int userCount=net.jforum.dao.DataAccessDriver.getInstance().newUserDAO().getTotalUsers();
		vc.put("unum",new Integer(userCount));
		//TODO
		//vc.put("anum",new Integer(DataAccessDriver.getInst().newAuthorsModel().countAll()));
		vc.put("snum",new Integer(DataAccessDriver.getInst().newStoriesDAO().countAll()));//  storiesPeer.count(new Criteria())));
		vc.put("tnum",new Integer(DataAccessDriver.getInst().newTopicsDAO().countAll()));// topicsPeer.count(new Criteria())));
		vc.put("cnum",new Integer(DataAccessDriver.getInst().newCommentDAO().countAll()));// commentsPeer.count(new Criteria())));
		vc.put("links",new Integer( DataAccessDriver.getInst().newLinkLinkDAO().countAll()));// links_linksPeer.count(new Criteria())));
		vc.put("cat",new Integer(DataAccessDriver.getInst().newLinkCategoriesDAO().countAll()));//  links_categoriesPeer.count(new Criteria())));
		vc.put("subnum",new Integer(DataAccessDriver.getInst().newQueueDAO().countAll()));//  queuePeer.count(new Criteria())));
		
		return new ActionForward(op,info);
	}
}
